package com.cindata

import grails.transaction.Transactional

@Transactional
class RiskWarningFlowStageNotificationService
{
    def initFlowStageNotification(RiskWarning riskWarning, Workflow workflow)
    {
        def workflowStages = WorkflowStage.findAllByWorkflow(workflow)
        for (
            workflowStage in
                workflowStages)
        {
            workflowStage?.notifications?.each {
                def riskWarningFlowStageNotification = new RiskWarningFlowStageNotification()
                riskWarningFlowStageNotification.role = it?.role
                riskWarningFlowStageNotification.messageTemplate = it?.messageTemplate
                riskWarningFlowStageNotification.toManager = it?.toManager
                riskWarningFlowStageNotification.cellphone = it?.cellphone
                riskWarningFlowStageNotification.flowStage = RiskWarningFlowStage.findByRiskWarningAndStage(riskWarning, workflowStage?.stage)
                if (riskWarningFlowStageNotification.validate())
                {
                    riskWarningFlowStageNotification.save flush: true
                }
                else
                {
                    println riskWarningFlowStageNotification.errors
                }
            }
        }
    }
}